package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.example.demo.mapper.BookMapper;
import com.example.demo.mapper.UserBookMapper;
import com.example.demo.model.Book;
import com.example.demo.model.User;
import com.example.demo.model.UserBook;
import com.example.demo.service.AdminService;
import com.example.demo.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private BookMapper bookMapper;
    @Autowired
    private UserBookMapper userBookMapper;

   @Override
   public Book addBook(Book book) {
       if(book == null) {
           throw new IllegalArgumentException("信息不能为空");
       }
       int insert = bookMapper.insert(book);
       if(insert == 1) {
           return book;
       }
       throw new RuntimeException("添加失败");
   }
   @Override
   public String deleteBook(Long bookId) {
       if (bookId == null) {
           throw new IllegalArgumentException("信息不能为空");
       }
       int delete = bookMapper.deleteById(bookId);
       if (delete == 1) {
           return "删除成功";
       }
       throw new RuntimeException("删除失败");
   }
       @Override
       public Book updateBook(Book book) {
           if(book == null) {
               throw new IllegalArgumentException("信息不能为空");
           }
           int update = bookMapper.updateById(book);
           if(update == 1) {
               return book;
           }
           throw new RuntimeException("修改失败");
       }
       @Override
       public Book getBookById(Long bookId) {
           if(bookId == null) {
               throw new IllegalArgumentException("信息不能为空");
           }
           return bookMapper.selectById(bookId);
       }
    @Override
    public PageVo<Book> getAllBooks(int page, int size) {
        Page<Book> bookPage = new Page<>(page, size);
        IPage<Book> bookIPage = bookMapper.selectPage(bookPage, null);
        return new PageVo<>(bookIPage);
    }

    @Override
    public PageVo<Book> searchBooks(Long bookId, String title, String author, int pageNumber, int pageSize) {
        Page<Book> pageParam = new Page<>(pageNumber, pageSize);
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        if (bookId != null) {
            queryWrapper.eq("book_id", bookId);
        }
        if (title != null && !title.isEmpty()) {
            queryWrapper.like("title", title);
        }
        if (author != null && !author.isEmpty()) {
            queryWrapper.like("author", author);
        }
        IPage<Book> bookIPage = bookMapper.selectPage(pageParam, queryWrapper);
        return new PageVo<>(bookIPage);
    }

    @Override
    public PageVo<UserBook> page(Long recordId, String title, User borrower, int pageNumber, int pageSize) {
        Page<UserBook> pageParam = new Page<>(pageNumber, pageSize);
        IPage<UserBook> userbookIPage = userBookMapper.selectUserBookByIdWithPaging(
                recordId,
                title,
                borrower != null ? borrower.getUsername() : null,
                pageParam
        );
        return new PageVo<>(userbookIPage);
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userBookMapper.findByUsername(username);
        if (user != null) {
            return new org.springframework.security.core.userdetails.User(user.getUsername(),
                    user.getPassword(), true, true, true, true, AuthorityUtils.createAuthorityList("USER"));
        } else {
            throw new UsernameNotFoundException("用户名不存在");
        }
    }


    @Override
    public Page<UserBook> getAllBorrowRecords(int page, int size) {
        Page<UserBook> userBookPage = new Page<>(page, size);
        return userBookMapper.selectPage(userBookPage, null);
    }
}